<?php

/**
 * Description of CategoryController
 *
 * @author gun
 */
require_once 'BaseController.php';
class Admin_CategoryController extends Admin_BaseController {
    
    public function init() {
        parent::init();
    }
    
    public function indexAction() {
        $request = $this->getRequest();                

        if ($this->httpRequest()->isXmlHttpRequest()) {
            $q = $request->getParam('q');
            $page = $request->getParam('page');
            
            $where = array('name LIKE ?'=>"%$q%");
            $stat = $this->_helper->ListPage('Admin_Model_CategoryService', $page, $where);            
            
            $options = array(
                'limit'=>
                    array(
                        $stat['perpage'] => $stat['offset'])
            );
            
            $service = AppRegistry::loadService('Admin_Model_CategoryService');
            $data = $service->getCategory($where, $options);
            
            if ($data) {
                $this->_helper->json(array(
                    'data' => $data,
                    'pview'=> $stat['pview'],
                    'offset'=> $stat['offset'],
                    'fieldname' => 'description'
                    ));
                
            } else {
                $this->_helper->json(array('error'=>'No Data Found.'));
            }
        }
    }
    
    public function addAction() {
        
    }
    
    public function editAction() {
        $id = $this->getParam('id');
        $service = AppRegistry::loadService('Admin_Model_CategoryService');
        $data = $service->getCategoryById($id);
        
        $this->view->data = $data;
    }
    
    public function saveAction() {
        $id = $this->getParam('id');
        $name = $this->getParam('name');
        $desc = $this->getParam('desc');
        
        $data = array(
            'name' => $name,
            'description' => $desc
        );
        
        foreach($this->baseUserLogin($id) as $key => $value) $data[ $key ] = $value;
        
        try {
            $service = AppRegistry::loadService('Admin_Model_CategoryService');
            $save = (!isset($id) ? $service->doSave($data) : $service->doUpdate($data, $id));                        
            
            $this->_helper->json(array('status'=>($save ? 'Save Success.' : 'Nothing changed.')));     
            
        } catch (Exception $exc) {
            $this->_helper->json(array('status'=>$exc->getMessage()));            
        }
        
        $this->_helper->viewRenderer->setNoRender();
    }
    
    public function deleteAction() {
        try {
            $id = $this->getParam('id');
            $service = AppRegistry::loadService('Admin_Model_CategoryService');            

            $delete = $service->doDelete($id);
            $this->_helper->json(array('status'=>($delete ? 'Delete success.' : 'Nothing deleted.')));
            
        } catch (Exception $e) {
            $this->_helper->json(array('status'=>$e->getTraceAsString()));
        }
    }
    
}

?>
